import BaseObj from "./BaseObj";
import Box from "./Box";
import Input from "./Input";
import MessageManager from "./MessageManager";
import Time from "./Time";
import MsgSend from "./msg/MsgSend";
import { move } from "./msg/msg";

export default class Player extends Box {
    userId: string
    isLocal: boolean
    moveSpeed: number = 50
    constructor(userId: string, isLocal: boolean = true) {
        super()
        this.userId = userId
        this.isLocal = isLocal
    }

    draw(ctx: CanvasRenderingContext2D): void {
        super.draw(ctx)
        ctx.textBaseline = 'middle'

        ctx.fillText(this.userId + "", this.x + 4, this.y + 10)
    }

    update(): void {
        if (!this.isLocal) return
        this.move()

    }

    move() {
        let horizontal = Input.Horizontal
        let vertical = Input.Vertical
        if (horizontal == 0 && vertical == 0) return//有移动，发送消息给其他player

        this.x += horizontal * this.moveSpeed * Time.frameTime
        this.y += vertical * this.moveSpeed * Time.frameTime
        //TODO 如果帧数太高，则每帧都发一个同步消息，太频繁了，需要改进

        let msg = {
            x: this.x,
            y: this.y,
            str: this.userId
        }
        MsgSend.send(move.C2S_SyncPosition,msg)
        // console.log(`有一定，发送位置同步消息：[${this.userId};${this.x};${this.y};${this.isLocal}]`);
        
    }
}